import { getEdgeMaxId } from "@/utils/antvx6/nodeOrEdgeMaxId";
import { edgeLabels, edgeAttrs, edgeRouter } from "@/utils/antvx6/configurationDetails";
/**
 * 可以通过isNew来判断连线完成， 对应的边是否创建新边
 * @param { Element }  elementGraph antv x6 元素
 */
export const connectedEdge = (elementGraph: any, lineMode: number) => {
  elementGraph.on("edge:connected", ({ isNew, edge }) => {
    if (isNew) {
      // 获取画布中唯一的边ID
      const edgeSum: number = getEdgeMaxId(elementGraph);
      const ids: string = `e${edgeSum}`;
      // 获取边的长度
      let lengths: number = 0;
      const view = elementGraph.findViewByCell(edge);
      const lengthFloor: number = view?.path?.length();
      lengths = Math.floor(lengthFloor * 100) / 100;
      // 给边设置属性
      edge.setData({
        edgeType: 1,
        lineMode,
        lengths,
        edgeName: ids,
      });
      edge.setProp({
        labels: edgeLabels(ids),
        attrs: edgeAttrs(2, lineMode),
        router: edgeRouter(),
      });
      elementGraph.updateCellId(edge, ids);
    }
  });
};
